package com.wonderful.mall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.wonderful.mall.order.entity.OrderEntity;
import com.wonderful.mall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wonderful.common.utils.PageUtils;
import com.wonderful.common.utils.Query;

import com.wonderful.mall.order.dao.OrderItemDao;
import com.wonderful.mall.order.entity.OrderItemEntity;
import com.wonderful.mall.order.service.OrderItemService;


@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnReasonEntity content, Channel channel){
        MessageProperties properties = message.getMessageProperties();
        long deliveryTag = properties.getDeliveryTag();
        try {
            //非批量模式
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            //退货  long deliveryTage,boolean multiple,boolean requeue
            try {
                channel.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
        System.out.println("接收到消息"+message+"内容"+content);
    }

    @RabbitHandler
    public void receiveMessage2(OrderEntity content){
        System.out.println("内容"+content);
    }

}